﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestFileUtils
    {
        [Test]
        public void TestCountFilesInDir()
        {
            FileUtils oFileUtils = new FileUtils();
            Assert.AreEqual(21, oFileUtils.CountFilesInDir(Constants.TestFiles_Control_CreateScripts_Dir));
        }
        [Test]
        public void TestCountFilesInDirRecursive()
        {
            FileUtils oFileUtils = new FileUtils();
            Assert.AreEqual(74, oFileUtils.CountFilesInDir(Constants.TestFiles_Control_CountFiles_Dir));

        }
        [Test]
        public void TestRenameFile()
        {
            FileUtils oFileUtils = new FileUtils();
            
            string strOldFileName = Constants.TestFiles_Output_FileUtils_Dir + "FirstFile.txt";
            oFileUtils.EnsureFileDirectoryExists (strOldFileName);
            oFileUtils.DeleteFilesInDir(Constants.TestFiles_Output_FileUtils_Dir);
            using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(strOldFileName))
            {
                oWriter.WriteLine("SomeText");
            }

            oFileUtils.RenameFile(strOldFileName, "RenamedFile.txt");

            Assert.IsTrue(System.IO.File.Exists(Constants.TestFiles_Output_FileUtils_Dir + "RenamedFile.txt"));
            Assert.IsFalse(System.IO.File.Exists(strOldFileName));
        }

        [Test]
        public void TestNewFile()
        {
            FileUtils oFileUtils = new FileUtils();

            string strNewFileName = Constants.TestFiles_Output_FileUtils_Dir + "FirstFile.txt";
            oFileUtils.EnsureFileDirectoryExists(strNewFileName);
            oFileUtils.DeleteFilesInDir(Constants.TestFiles_Output_FileUtils_Dir);

            string strCreatedFileName = oFileUtils.NewFile(strNewFileName);
            Assert.AreEqual(strNewFileName, strCreatedFileName);

            Assert.IsTrue(System.IO.File.Exists(Constants.TestFiles_Output_FileUtils_Dir + "FirstFile.txt"));
            //Assert.IsFalse(System.IO.File.Exists(strNewFileName));
            oFileUtils.NewFile(strNewFileName);
            Assert.IsTrue(System.IO.File.Exists(Constants.TestFiles_Output_FileUtils_Dir + "FirstFile1.txt"));
            oFileUtils.NewFile(strNewFileName);
            Assert.IsTrue(System.IO.File.Exists(Constants.TestFiles_Output_FileUtils_Dir + "FirstFile2.txt"));

        }

        [Test]
        public void TestSetFileWritable()
        {
            FileUtils oFileUtils = new FileUtils();
            string strNewFileName = Constants.TestFiles_Output_FileUtils_Dir + "ReadOnly.txt";
            oFileUtils.EnsureFileDirectoryExists(strNewFileName);
            oFileUtils.DeleteFilesInDir(Constants.TestFiles_Output_FileUtils_Dir);

            string strCreatedFileName = oFileUtils.NewFile(strNewFileName);
            Assert.AreEqual(strNewFileName, strCreatedFileName);

            System.IO.FileInfo oFileInfo = new System.IO.FileInfo(strNewFileName);
            
            oFileInfo.IsReadOnly = true;

            System.IO.FileInfo oCheckReadOnly = new System.IO.FileInfo(strNewFileName);
            Assert.IsTrue(oCheckReadOnly.IsReadOnly);

            FileUtils.EnsureFileIsWriteable(strNewFileName);

            System.IO.FileInfo oCheckWriteable = new System.IO.FileInfo(strNewFileName);
            Assert.IsFalse(oCheckWriteable.IsReadOnly);


        }
    }
}
